#include "ui_menu.h"
#include "ui.h"
#include "lvgl/lvgl.h"
#include "theme.h"
#include "lang.h"
#include "global.h"


struct menu_layout
{
    lv_obj_t *cont;
};
static struct menu_layout g_menu_layout = {0};


static void menu_btn_return_event(lv_event_t *event)
{
    ui_enter(E_UI_MONITOR_DATA, E_UI_DATA_PM2D5);
}

static void menu_btn_monitor_event(lv_event_t *event)
{
    ui_enter(E_UI_MONITOR_CHART, lv_event_get_user_data(event));
}

static void menu_btn_setting_event(lv_event_t *event)
{
    ui_enter(E_UI_SETTING, lv_event_get_user_data(event));
}

static lv_obj_t *menu_btn_create(lv_obj_t *parent, const char *text, 
    lv_coord_t x, lv_coord_t y, lv_event_cb_t event_cb, void * user_data)
{
    lv_obj_t *btn = lv_btn_create(parent);
    lv_obj_add_style(btn, &theme_styles->style_btn_circular, LV_PART_MAIN | LV_STATE_DEFAULT);
    lv_obj_set_size(btn, 75, 75);
    lv_obj_set_pos(btn, x, y);
    if (event_cb)
    {
        lv_obj_add_event_cb(btn, event_cb, LV_EVENT_CLICKED, user_data);
    }

    lv_obj_t *lb = lv_label_create(btn);
    lv_obj_add_style(lb, &theme_styles->style_btn_name, LV_PART_MAIN | LV_STATE_DEFAULT);
    lv_label_set_text(lb, text);
    lv_obj_align(lb, LV_ALIGN_CENTER, 0, 0);

    return btn;
}

void ui_menu_init(void *para)
{
    struct menu_layout *layout = &g_menu_layout;

    memset(layout, 0, sizeof(struct menu_layout));
    layout->cont = lv_obj_create(lv_scr_act());
    lv_obj_add_style(layout->cont, &theme_styles->style_bg_black, LV_PART_MAIN | LV_STATE_DEFAULT);
    lv_obj_set_size(layout->cont, lv_disp_get_hor_res(NULL), lv_disp_get_ver_res(NULL));

    lv_obj_t *btn_img = lv_btn_create(layout->cont);
    lv_obj_add_style(btn_img, &theme_styles->style_btn_opa0, LV_PART_MAIN | LV_STATE_DEFAULT);
    lv_obj_set_size(btn_img, 60, 60);
    lv_obj_align(btn_img, LV_ALIGN_TOP_LEFT, 0, 0);
    lv_obj_add_event_cb(btn_img, menu_btn_return_event, LV_EVENT_CLICKED, para);
    
    lv_obj_t *img_return = lv_img_create(btn_img);
    lv_img_set_src(img_return, UI_RETURN_ICON_PATH);
    lv_obj_align(img_return, LV_ALIGN_CENTER, 0, 0);

    menu_btn_create(layout->cont, lang_disp_str(E_LAN_DISP_PM2DOT5), 
        64, 87, menu_btn_monitor_event, (void *)E_UI_CHART_PM2D5);
    menu_btn_create(layout->cont, lang_disp_str(E_LAN_DISP_PM1), 
        205, 87, menu_btn_monitor_event, (void *)E_UI_CHART_PM1);
    menu_btn_create(layout->cont, lang_disp_str(E_LAN_DISP_PM10), 
        346, 87, menu_btn_monitor_event, (void *)E_UI_CHART_PM10);

    menu_btn_create(layout->cont, lang_disp_str(E_LAN_DISP_FORMALDEHYDE), 
        64, 215, menu_btn_monitor_event, (void *)E_UI_CHART_FORMALDEHYDE);
    menu_btn_create(layout->cont, lang_disp_str(E_LAN_DISP_TVOC), 
        205, 215, menu_btn_monitor_event, (void *)E_UI_CHART_TVOC);
    menu_btn_create(layout->cont, lang_disp_str(E_LAN_DISP_CO2), 
        345, 215, menu_btn_monitor_event, (void *)E_UI_CHART_CO2);

    menu_btn_create(layout->cont, lang_disp_str(E_LAN_DISP_TEMPERATURE), 
        64, 345, menu_btn_monitor_event, (void *)E_UI_CHART_TEMP);
    menu_btn_create(layout->cont, lang_disp_str(E_LAN_DISP_HUMIDITY), 
        205, 345, menu_btn_monitor_event, (void *)E_UI_CHART_HUMIDITY);
    menu_btn_create(layout->cont, lang_disp_str(E_LAN_DISP_SETTING), 
        346, 345, menu_btn_setting_event, NULL);
}

void ui_menu_exit(void)
{
    struct menu_layout *layout = &g_menu_layout;

    if (layout->cont)
    {
        lv_obj_del(layout->cont);
        layout->cont = NULL;
    }
}

